package com.meviewer.common;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author hanhaoran
 * @date 2020/4/12
 */
public final class Times {
    private Times() {}

    public static Date trans2Date(Calendar calendar) {
        return Date.from(calendar.toInstant());
    }

    public static Date trans2Date(Instant instant) {
        return Date.from(instant);
    }

    public static Date trans2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date trans2Date(ZonedDateTime zonedDateTime) {
        return Date.from(zonedDateTime.toInstant());
    }

    public static Calendar trans2Calendar(Date date) {
        return GregorianCalendar.from(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    }

    public static Calendar trans2Calendar(Instant instant) {
        return GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
    }

    public static Calendar trans2Calendar(LocalDateTime localDateTime) {
        return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
    }

    public static Calendar trans2Calendar(ZonedDateTime zonedDateTime) {
        return GregorianCalendar.from(zonedDateTime);
    }

    public static Instant trans2Instant(Date date) {
        return date.toInstant();
    }

    public static Instant trans2Instant(Calendar calendar) {
        return calendar.toInstant();
    }

    public static Instant trans2Instant(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
    }

    public static Instant trans2Instant(ZonedDateTime zonedDateTime) {
        return zonedDateTime.toInstant();
    }

    public static LocalDateTime trans2LocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime trans2LocalDateTime(Calendar calendar) {
        return calendar.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime trans2LocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static LocalDateTime trans2LocalDateTime(ZonedDateTime zonedDateTime) {
        return zonedDateTime.toLocalDateTime();
    }

    public static LocalDate trans2LocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate trans2LocalDate(Calendar calendar) {
        return calendar.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate trans2LocalDate(Instant instant) {
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate trans2LocalDate(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate();
    }

    public static LocalDate trans2LocalDate(ZonedDateTime zonedDateTime) {
        return zonedDateTime.toLocalDate();
    }

    public static LocalTime trans2LocalTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalTime trans2LocalTime(Calendar calendar) {
        return calendar.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalTime trans2LocalTime(Instant instant) {
        return instant.atZone(ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalTime trans2LocalTime(LocalDateTime localDateTime) {
        return localDateTime.toLocalTime();
    }

    public static LocalTime trans2LocalTime(ZonedDateTime zonedDateTime) {
        return zonedDateTime.toLocalTime();
    }
}
